let url_js = require("../../utils/url.js");
let api = require("../../utils/apiRequest.js");
let common_js = require("../../utils/common.js");
let en_js = require('../../utils/environment.js');
let page_js = require("../../utils/pageOptions.js");
const {
    couponListUrl
} = require("../../utils/url.js");

Page({
    data: {
        dialog: false,
        id: '',
        orderId: '',
        info: '',
        desText: [],
        shareCouponUrl: url_js.imgUrl + "share-coupon.jpg",
    },
    ctrlDialog() {
        this.setData({
            dialog: !this.data.dialog
        })
    },
    cancel() {
        //选择取消
        wx.reLaunch({
            url: '../index/index'
        });
        this.ctrlDialog();
    },
    confirm() {
        //去自提点
        wx.reLaunch({
            url: '../indexSearchShop/indexSearchShop'
        });
        this.ctrlDialog()
    },
    goHome() {
        wx.reLaunch({
            url: '../index/index'
        });
    },
    refresh() {
        let self = this;
        api.wxRequest(url_js.refreshsharecoupon, {
            orderId: this.data.orderId
        }).then(res => {
            if (res.code === 200) {

                //生成使用说明信息
                let tmpDesText = [];
                let type = res.result.currentUserCoupon.productRange.type;
                if (type == 0) {
                    tmpDesText.push("1.适用全部商品（活动商品除外），以下单时为准")
                } else {
                    tmpDesText.push("1.适用部分商品（不含活动商品），以下单时为准")
                }
                tmpDesText.push("2.该券与全部促销活动不可叠加使用");
                let type2 = res.result.currentUserCoupon.siteRange.type;
                if (type2 == 0) {
                    tmpDesText.push("3.全部站点可用，以下单时为准")
                } else {
                    tmpDesText.push("3.部分站点可用，以下单时为准")
                }
                this.setData({
                    info: res.result,
                    desText: tmpDesText
                })
                // return
                // let status = res.result.dealCampaignShareCouponVo.participantStatus;
                // if(status == 0 || status == 2){

                // }else{
                //     wx.hideLoading()
                //     wx.showModal({
                //         title:'友情提醒',
                //         content:status==1?'活动失效':status==12?'您已领取':status==3?'已领光':status==4?'老用户不能参与':'不能邀请自己',
                //         complete(){
                //             self.goHome()
                //         }
                //     })
                // }
            }
        }).catch(err => {
            common_js.log(err);
        });
    },
    pick() {
        //领取红包动作
        wx.showLoading({
            title: '正在加载...',
            mask: true
        }, 3000);
        let self = this;
        api.wxRequest(url_js.pick, {
            orderId: this.data.orderId
        }).then(res => {
            if (res.code == 200) {
                let status = res.result.dealCampaignShareCouponVo.participantStatus;
                if (status == 0 || status == 2) {
                    this.setData({
                        info: res.result
                    })
                    //若领取成功，则调动refresh方法刷新一下，拉取信息
                    this.refresh();
                } else {
                    wx.hideLoading()
                    wx.showModal({
                        title: '友情提醒',
                        content: status == 1 ? '活动失效' : status == 3 ? '已领光' : status == 4 ? '分享链接已过期' : '可领数量已达上线',
                        complete() {
                            self.goHome()
                        }
                    })
                }
            }
        }).catch(err => {
            common_js.log(err);
        });
    },
    goHome() {
        wx.reLaunch({
            url: '../index/index'
        });
    },
    stop() {
        return false
    },
    onShow() {
        setTimeout(() => {
            let userInfo = wx.getStorageSync(en_js.userInfoKey);
            if (!userInfo || (userInfo && !userInfo.wxUserInfo) || (userInfo && !userInfo.ztd)) {
                //弹窗，让用户选择去获取位置，微信授权，选择自提点
                page_js.savePageOptionsInfo({
                    url: '../shareCoupon/shareCoupon',
                    param: `?orderId=${this.data.orderId}`
                });
                wx.reLaunch({
                    url: '../indexSearchShop/indexSearchShop'
                });
                return
            }
            if (userInfo && !userInfo.userInfo) {
                //没有手机号码授权登录，跳转到手机号码登录页
                page_js.savePageOptionsInfo({
                    url: '../shareCoupon/shareCoupon',
                    param: `?orderId=${this.data.orderId}`
                });
                wx.navigateTo({
                    url: '../loginIndex/loginIndex'
                });
                return
            }
            this.pick();
        }, 50)
    },
    onLoad: function (op) {
        this.setData({
            orderId: Number(op.orderId)
        })
    },
    stop() {
        return false
    }
})